package cn.jasonone.tio.tcp.config;

import cn.jasonone.tio.tcp.commons.CommandFactory;
import cn.jasonone.tio.tcp.commons.impl.SimpleCommandFactory;
import cn.jasonone.tio.tcp.handler.DataAioHandler;
import cn.jasonone.tio.tcp.handler.EncryptionHandler;
import cn.jasonone.tio.tcp.handler.SerializationHandler;
import cn.jasonone.tio.tcp.handler.impl.DefaultEncryptionHandler;
import cn.jasonone.tio.tcp.handler.impl.HeartbeatCommandHandler;
import cn.jasonone.tio.tcp.handler.impl.JdkSerializationHandler;
import cn.jasonone.tio.tcp.listener.GlobalAioListener;
import cn.jasonone.tio.tcp.listener.GlobalIpStatListener;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author xujie
 * @version 1.0
 * @date 2022/1/19 8:09
 */
@Slf4j
@ComponentScan("cn.jasonone.tio.tcp")
public class TioConfig {
    private static final Marker MARKER = MarkerFactory.getMarker("TIO-CONFIG");

    @Bean
    public GlobalAioListener aioListener() {
        return new GlobalAioListener();
    }

    @Bean
    public GlobalIpStatListener ipStatListener() {
        return new GlobalIpStatListener();
    }

    @Bean
    public DataAioHandler aioHandler(SerializationHandler serializationHandler, CommandFactory commandFactory, EncryptionHandler encryptionHandler) {
        DataAioHandler dataAioHandler = new DataAioHandler(serializationHandler, commandFactory, encryptionHandler);
        return dataAioHandler;
    }

    @Bean
    @ConditionalOnMissingBean(GlobalAioListener.class)
    public GlobalAioListener globalAioListener() {
        return new GlobalAioListener();
    }

    @Bean
    @ConditionalOnMissingBean(HeartbeatCommandHandler.class)
    public HeartbeatCommandHandler heartbeatCommandHandler() {
        return new HeartbeatCommandHandler();
    }

    @Bean
    @ConditionalOnMissingBean(SerializationHandler.class)
    public SerializationHandler serializationHandler() {
        log.warn(MARKER, "未配置序列化处理器,将使用默认序列化处理器:{}", JdkSerializationHandler.class);
        return new JdkSerializationHandler();
    }

    @Bean
    @ConditionalOnMissingBean(CommandFactory.class)
    public CommandFactory commandFactory() {
        log.warn(MARKER, "未配置命令工厂,将使用默认命令工厂:{}", SimpleCommandFactory.class);
        return new SimpleCommandFactory();
    }

    @Bean
    @ConditionalOnMissingBean(EncryptionHandler.class)
    public EncryptionHandler encryptionHandler() {
        log.warn(MARKER, "未配置加解密处理器,将使用默认加解密处理器:{}", DefaultEncryptionHandler.class);
        return new DefaultEncryptionHandler();
    }


}
